package com.team.six.bluecatshop.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.team.six.bluecatshop.domain.Address;
import com.team.six.bluecatshop.domain.Customer;
import com.team.six.bluecatshop.domain.CustomerExample;
import com.team.six.bluecatshop.domain.SeachCustomerResponse;
import com.team.six.bluecatshop.mapper.AddressMapper;
import com.team.six.bluecatshop.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    @Transactional
    public void add(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    @Transactional
    public void remove(String id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Customer findById(String id) {
        Customer customer=customerMapper.selectByPrimaryKey(id);
        return customer;
    }

    @Override
    public List<Customer> findAll() {
        List<Customer> customerList = customerMapper.findAll();
        return customerList;
    }

    @Override
    public Customer findCustomerByName(String name) {
        Customer customer = customerMapper.findByName(name);
        return customer;
    }

    @Override
    public Customer findCustomerByPhone(String phone) {
        Customer customer = customerMapper.findByPhone(phone);
        return customer;
    }

    @Override
    @Transactional
    public void updateByPrimaryKeySelective(Customer customer) {
        customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public Customer findCustomerByEmail(String customerEmail) {
        Customer customer = customerMapper.findByEmail(customerEmail);
        return customer;
    }

    @Override
    public SeachCustomerResponse findCustomerAll(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameLike("%"+keyword+"%");
        List<Customer> customers = customerMapper.selectByExample(example);
        return new SeachCustomerResponse(((Page)customers).getTotal(),customers);
    }
}
